package com.mybook.app.service;

import com.github.pagehelper.PageInfo;
import com.mybook.app.core.BaseResponse;
import com.mybook.app.core.UserContext;
import com.mybook.app.core.WebPageInfo;
import com.mybook.app.dto.BevaInfoAndUserInfoDto;
import com.mybook.app.exception.DataIsEmptyException;
import com.mybook.app.mapper.BevaInfoMapper;
import com.mybook.app.mapper.UevaInfoMapper;
import com.mybook.app.mapper.UserInfoMapper;
import com.mybook.app.model.BevaInfo;
import com.mybook.app.model.UevaInfo;
import com.mybook.app.model.UserInfo;
import com.mybook.app.request.CommentAddRequest;
import com.mybook.app.request.CommentPeopleRequest;
import com.mybook.app.request.CommentRequest;
import com.mybook.app.utils.IdUtils;
import com.mybook.app.utils.Logger;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

/**
 * Created by zjutsw on 2017/11/8.
 */
@Service
public class CommentService {
    @Autowired
    private BevaInfoMapper bevaInfoMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UevaInfoMapper uevaInfoMapper;
    @Autowired
    private UserService userService;

    public void add(CommentAddRequest commentAddRequest) {
        Logger.debug(this, "添加评论");
        //UserInfo userInfo = userInfoMapper.findUserInfoById(UserContext.getCurrentuserId().get());
        BevaInfo bevaInfo = new BevaInfo();
        //bevaInfo.setId(commentAddRequest.getId());
        bevaInfo.setId(IdUtils.getId(userService.getCity()));
        bevaInfo.setuId(UserContext.getCurrentuserId().get());
        bevaInfo.setbId(commentAddRequest.getbId());
        bevaInfo.setcTime(new Date());
        bevaInfo.setuTime(new Date());
        bevaInfo.setStar(commentAddRequest.getStar());
        try {
            String detail = URLDecoder.decode(commentAddRequest.getDetail(), "utf-8");
            bevaInfo.setDetail(detail);
        } catch (UnsupportedEncodingException ex) {
            Logger.warn(this, "转码错误", ex);
            bevaInfo.setDetail(commentAddRequest.getDetail());
        }

        Integer row = bevaInfoMapper.insertSelective(bevaInfo);
        Logger.info(this, "添加评论是否成功：" + row);
    }


    public void people(CommentPeopleRequest commentPeopleRequest) {
        Logger.debug(this, "添加评论");
        //UserInfo userInfo = userInfoMapper.findUserInfoById(UserContext.getCurrentuserId().get());
        UevaInfo uevaInfo = new UevaInfo();
        //uevaInfo.setId(commentPeopleRequest.getId());
        uevaInfo.setId(IdUtils.getId(userService.getCity()));
        uevaInfo.setuId(UserContext.getCurrentuserId().get());
        uevaInfo.setAuId(commentPeopleRequest.getAuId());
        uevaInfo.setcTime(new Date());
        uevaInfo.setuTime(new Date());
        uevaInfo.setFlag(commentPeopleRequest.getFlag());
        uevaInfo.setStar(commentPeopleRequest.getStar());
        Integer row = uevaInfoMapper.insertSelective(uevaInfo);
        Logger.info(this, "添加流转人评论是否成功：" + row);
    }

    public BaseResponse book(CommentRequest commentAddRequest) {
        Logger.debug(this, "获取评论");
        List<BevaInfoAndUserInfoDto> bevaInfoAndUserInfoDtos = bevaInfoMapper.findBevaInfoByBId(commentAddRequest);

        if (CollectionUtils.isEmpty(bevaInfoAndUserInfoDtos)) {
            Logger.info(this, "查询数据为空");
            throw new DataIsEmptyException();
        }
        //分页数据封装
        PageInfo<BevaInfoAndUserInfoDto> pageInfo = new PageInfo<>(bevaInfoAndUserInfoDtos);
        WebPageInfo webPageInfo = new WebPageInfo();
        BeanUtils.copyProperties(pageInfo, webPageInfo);

        BaseResponse baseResponse = new BaseResponse(bevaInfoAndUserInfoDtos, webPageInfo);
        return baseResponse;
    }


}
